using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEditor;
using UnityEngine;

namespace DT_URP_Studio
{

    [ExecuteInEditMode]
    public class SetShaderDirWS_div : MonoBehaviour
    {
        [SerializeField] Renderer m_renderer;

        [SerializeField] Transform HeadTransform;


        [SerializeField] bool isViewHandle = false;
        [SerializeField] Vector3 lcoalaxialZ = Vector3.forward;
        [SerializeField] Vector3 lcoalaxialY = Vector3.up;
        // [SerializeField] Vector3 lcoalaxialX = Vector3.right;
        [SerializeField] float handleSize = 1;

        [Header("View World Axial")]
        [SerializeField] Vector3 axialZ = Vector3.forward;

        [SerializeField] Vector3 axialY = Vector3.up;
        [SerializeField] Vector3 axialX = Vector3.right;

        // Update is called once per frame
        void Update()
        {
            GetAxial();
            SetRendererForDir();
        }
        void OnDrawGizmos()
        {
            if (!isViewHandle)
            {
                return;
            }
            Gizmos.color = Color.blue;
            Gizmos.DrawLine(HeadTransform.position, HeadTransform.position + axialZ * handleSize);
            Gizmos.DrawSphere(HeadTransform.position + axialZ * handleSize, 0.05f);

            Gizmos.color = Color.green;
            Gizmos.DrawLine(HeadTransform.position, HeadTransform.position + axialY * handleSize);
            Gizmos.DrawSphere(HeadTransform.position + axialY * handleSize, 0.05f);

            Gizmos.color = Color.red;
            Gizmos.DrawLine(HeadTransform.position, HeadTransform.position + axialX * handleSize);
            Gizmos.DrawSphere(HeadTransform.position + axialX * handleSize, 0.05f);
        }
        void GetAxial()
        {
            axialZ = this.HeadTransform.TransformDirection(lcoalaxialZ.normalized);
            axialY = this.HeadTransform.TransformDirection(lcoalaxialY.normalized);
            axialX = Vector3.Cross(axialY, axialZ);
        }
        void SetRendererForDir()
        {
            if (m_renderer == null)
            {
                return;
            }
            Material[] materials = m_renderer.sharedMaterials;
            for (int i = 0; i < materials.Length; i++)
            {
                materials[i].SetVector("_upDirWS", axialY);
                materials[i].SetVector("_frontDirWS", axialZ);
                materials[i].SetVector("_rightDirWS", axialX);
            }
            m_renderer.materials = materials;
        }
    }
}